package com.wing.warehouse.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.warehouse.model.entity.PalletBoxList;
import com.wing.warehouse.model.response.PalletBoxListVO;
import com.wing.warehouse.service.PalletBoxListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.warehouse.model.request.PalletBoxListForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-托盘料箱清单管理", tags = "托盘料箱清单管理")
@RestController("appPalletBoxListController")
@RequestMapping("warehouse/app/palletBoxList")
public class PalletBoxListController {

    @Autowired
    private PalletBoxListService palletBoxListService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询所有料箱清单", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<PalletBoxListVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String code
            ,String spaceCodeFlag
            ,Long vehicleTypeId
            ,Long warehouseId
            ,Long areaId
            ,Long shelfId
            ,String spaceCode
    ) {
        Page<PalletBoxListVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        params.put("spaceCodeFlag", spaceCodeFlag);
        params.put("vehicleTypeId", vehicleTypeId);
        params.put("warehouseId", warehouseId);
        params.put("areaId", areaId);
        params.put("shelfId", shelfId);
        params.put("spaceCode", spaceCode);
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<PalletBoxListVO> list = palletBoxListService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long palletBoxListId) {
        PalletBoxListVO palletBoxListVO = palletBoxListService.get4appById(palletBoxListId);
        if (palletBoxListVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), palletBoxListVO);
    }

    @PostMapping()
    public JsonResult add(@Validated PalletBoxListForm palletBoxListForm) {
        if (palletBoxListService.add(palletBoxListForm)) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @PutMapping()
    public JsonResult update(PalletBoxListForm palletBoxListForm) {
        PalletBoxList palletBoxList =
            palletBoxListService.getOne(new QueryWrapper<PalletBoxList>().eq("id" , palletBoxListForm.getId()));
        if (palletBoxList ==null){
            return JsonResult.fail(localeMessage.getMessage("common.fail" ));
        }
        if (palletBoxListService.updateById(palletBoxList)) {
            return JsonResult.success(localeMessage.getMessage("common.success" ));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail" ));
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long palletBoxListId) {
        PalletBoxList palletBoxList =palletBoxListService.getById(palletBoxListId);
        if (palletBoxList ==null){
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        if (palletBoxListService.removeById(palletBoxListId)) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "获取料箱数据", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "料箱code", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getBoxByCode")
    public JsonResult<PalletBoxListVO> getPalletBoxByCode(String code){
        return JsonResult.success(palletBoxListService.getPalletBox(code,null));
    }

    @ApiOperation(value = "获取料箱数据(根据配送单详情id)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliveryDetailId", value = "配送单明细id", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getBoxByDeliveryDetailId")
    public JsonResult<List<PalletBoxListVO>> getBoxByDeliveryDetailId(Long deliveryDetailId){
        return JsonResult.success(palletBoxListService.getBoxByDeliveryDetailId(deliveryDetailId));
    }

}
